package com.qf.controller;

import com.qf.pojo.entity.TbUser;
import com.qf.pojo.req.TbUserReq;
import com.qf.pojo.req.TbUserRoleReq;
import com.qf.pojo.resp.BaseResp;
import com.qf.service.TbUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api("用户控制层")
@RestController
@RequestMapping("/user")
public class TbUserController {
    @Autowired
    TbUserService tbUserService;
    @RequiresPermissions({"USER_FIND_ALL"})
    @ApiOperation(value = "查询所有用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "size",value = "当前页显示行数",required = true,dataType = "Integer")
    })
    @GetMapping("/findAll")
    public BaseResp findAll(@RequestParam("page") Integer page,@RequestParam("size")Integer size){
        return tbUserService.findAll(page,size);
    }
    @RequiresPermissions({"USER_DELETE"})
    @ApiOperation(value = "删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tbUserReq",value = "请求的用户对象",required = true,dataType = "TbUserReq")
    })
    @PostMapping("/deleteById")
    public BaseResp deleteById(@RequestBody TbUserReq tbUserReq){
        return tbUserService.deleteById(tbUserReq.getId());
    }
    @RequiresPermissions({"USER_UPDATE_OR_SAVE"})
    @ApiOperation(value = "修改或新增用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tbUserReq",value = "请求的用户对象",required = true,dataType = "TbUserReq")
    })
    @PostMapping("/updateOrSave")
    public BaseResp updateOrSave(@RequestBody TbUserReq tbUserReq){
        return tbUserService.updateOrSave(tbUserReq);
    }
    @ApiOperation("通过用户id查询用户对应的角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "map",value = "前端传来的用户id",required = true,dataType = "Map")
    })
    @PostMapping("/findRoleByUserId")
    public BaseResp findRoleByUserId(@RequestBody Map map){
        return tbUserService.findRoleByUserId(Integer.valueOf(map.get("id").toString()));
    }
    @ApiOperation(value = "修改用户角色或新增用户角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tbUserRoleReq",value = "用户角色请求对象",required = true,dataType = "TbUserRoleReq")
    })
    @PostMapping("/updateUserRole")
    public BaseResp updateUserRole(@RequestBody TbUserRoleReq tbUserRoleReq){
        return tbUserService.updateUserRole(tbUserRoleReq);
    }
    @ApiOperation(value = "登录操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tbUserReq",value = "用户请求对象",required = true,dataType = "TbUserReq")
    })
    @PostMapping("/login")
    public BaseResp login(@RequestBody TbUserReq tbUserReq){
        return tbUserService.login(tbUserReq);
    }
    @ApiOperation(value = "判断用户是否登录")
    @PostMapping("/isAuthentication")
    public BaseResp isAuthentication (){
        //获取对象
        Subject subject = SecurityUtils.getSubject();
        //判断是否登录
        if (subject.isAuthenticated()){
            return new BaseResp(1,"success",subject.getPrincipal(),null);
        }
        return new BaseResp().ERROR();
    }
    @ApiOperation(value = "退出登录")
    @PostMapping("/loginOut")
    public BaseResp loginOut(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new BaseResp().SUCCESS();
    }
}
